package com.jack.bookshop.util;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class PasswordUtil {

    // MD5加密
    public static String encryptByMD5(String password) {
        String str = "";
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(password.getBytes());
            byte[] bytes = messageDigest.digest();
            str = String.format("%032x", new BigInteger(1, bytes));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    // SHA加密
    public static String encryptBySHA(String password) {
        String str = "";
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA");
            messageDigest.update(password.getBytes());
            str = new BigInteger(messageDigest.digest()).toString(32);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return str;
    }

    // 测试一下加密效果
    public static void main(String[] args) {
        String s1 = "123456";
        System.out.println(PasswordUtil.encryptByMD5(s1));
        System.out.println(PasswordUtil.encryptBySHA(s1));
    }

}

